/*
* Description: jqGrid extention
* Dependency: jQuery/jQuery UI/jqGrid 
* Copyright (c) IBM
* Date: 2010-10-10
* Author: zhuwei
*/

;(function($){
	// .gridUtil() interface
	$.fn.gridUtil = function(jq){
		if( this instanceof $.fn.gridUtil ){
			this.$ = jq;
			return this;
		}
		return new $.fn.gridUtil(this);
	};
	// $.gridUtil(selector) interface
	$.gridUtil = function(selector){
		return new $.fn.gridUtil($(selector));
	};

	$.fn.gridUtil.prototype = $.fn.gridUtil.fn = {
		defaults : {
			datatype: "json",
			mtype: "POST",
			height: "auto",
			autowidth: true,
			shrinkToFit: false,
			sortable: true,
			altRows: true,
			altclass: 'altClass',
			multiboxonly: true,
			rowNum: 20,
			rowList: [10,20,30,50,100],
			multiselect: true,
			viewrecords: true,
			rownumbers: true,
			prmNames: {page: "pageParams.currentPage", rows: "pageParams.pageSize", sort: "pageParams.sortName", order: "pageParams.sortOrder", nd: "pageParams.random", search: "pageParams.search"},
			//autoencode: true,
			loadError : function(xhr,st,err) {
				$.boxUtil.error('查询异常');
			},
			jsonReader: {
				root: "rows",
				page: "currentPage",
				total: "totalPages",
				records: "totalRows",
				repeatitems : false,
				subgrid: { 
					root: "rows",
					page: "currentPage",
					total: "totalPages",
					records: "totalRows", 
					repeatitems : false
				}
			}
		},
		defaultParams : {
			heightspan : 300
		},
		treeOptions : {
			treeGridModel : 'adjacency',
			treeGrid: true
			//ExpandColumn: 'name',
			//ExpandColClick: true
		},
		/*
		 * options : jqGrid options
		 * params : { 
		 *             singleselect : true  // could select only one row
		 *             lazyload : true  // will not load data UI onload
		 *             heightfixed : true // will fix height and freeze row header
		 *             heightspan : 300 // default is 300
		 *             pageCache : true // cache the page search conditions
		 *          }
		 */
		simpleGrid: function(options, params) {
			var opts = {};

			var parameters = $.extend({}, this.defaultParams, params);
			
			// page cache
			if (parameters.pageCache == true) {
				// load cache
				if (window.location.search.indexOf("loadPageCache=true") >= 0) {
					var cache;
					try {						
						cache = JSON.parse($.cookie(window.location.pathname));						
					} catch(e){ };
					
					if (typeof(cache) != 'undefined' && cache != null) {
						opts.page = cache['page'];
						opts.rowNum = cache['rowNum'];
						opts.sortname = cache['sortname'];
						opts.sortorder = cache['sortorder'];
						opts.postData = cache['postData'];
						
						for(prop in opts.postData) {
							$("[name='" + prop + "']").val(opts.postData[prop]);
						}
					}
				}
				
				// save cache
				var g = options.loadComplete;
				options.loadComplete = function(result) {
					_loadComplete(result);
					
					if (typeof(g) != 'undefined' && g != null) {
						g.call(result);
					}
					
					var jq = $(this);
					var cachePostData = {};
					var postData = jq.jqGrid('getGridParam','postData');
					for (prop in postData) {
						if (prop.indexOf("pageParams") < 0) {
							cachePostData[prop] = postData[prop];
						}
					}
					
					var cache = {
						'page': jq.jqGrid('getGridParam','page'),
						'rowNum': jq.jqGrid('getGridParam','rowNum'),
						'sortname': jq.jqGrid('getGridParam','sortname'),
						'sortorder': jq.jqGrid('getGridParam','sortorder'),
						'postData': cachePostData
					};
					
					var date = new Date();
					var m = 20;
					date.setTime(date.getTime() + (m * 60 * 1000));
					
					$.cookie(window.location.pathname, JSON.stringify(cache), {expires: date});
				}
			} else {
				var g = options.loadComplete;
				options.loadComplete = function(result) {
					_loadComplete(result);
					
					if (typeof(g) != 'undefined' && g != null) {
						g.call(result);
					}					
				}
			}
						
			// pagination check
			if (typeof(options.onPaging) == 'function') {
				var g = options.onPaging;
				options.onPaging = function(p) {
					var jq = $(this);
					if (p == 'user') {
						var pager = jq.getGridParam("pager");
						var jump = $(pager + " .ui-pg-input").val();
						if (!jump.isNumber()) {
							$.boxUtil.alert('请输入数字！');
							return 'stop';
						}
					}
					
					g.call();
				}
			} else {
				options.onPaging = function(p) {
					var jq = $(this);
					if (p == 'user') {
						var pager = jq.getGridParam("pager");
						var jump = $(pager + " .ui-pg-input").val();
						if (!jump.isNumber()) {
							$.boxUtil.alert('请输入数字！');
							return 'stop';
						}
					}
				}
			}

			// singleselect
			if (parameters.singleselect) {
				if (typeof(options.gridComplete) == 'function') {
					var g = options.gridComplete;
					options.gridComplete = function() {
						g.call();
						$("[aria-describedby='" + this.id + "_cb'] > input").attr("disabled", true);
						$("#cb_" + this.id).hide();
					}
				} else {
					options.gridComplete = function() {
						$("[aria-describedby='" + this.id + "_cb'] > input").attr("disabled", true);
						$("#cb_" + this.id).hide();
					}
				}
			}
			
			// lazyload
			if (parameters.lazyload) {
				opts.datatype = "local";
				opts.url = "";
			}

			var settings = $.extend({}, this.defaults, options, opts);
			var jq = this.$.jqGrid(settings);
			
			// lazyload
			if (parameters.lazyload) {
				jq.jqGrid("setGridParam",{url: options.url, datatype: "json"});
			}
			
			// heightfixed
			if (parameters.heightfixed) {
				$(window).bind('resize', function() {
					jq.jqGrid("setGridHeight", $(window).height()- parameters.heightspan );
				}).trigger('resize');
			}

			if(!$.support.leadingWhitespace) {
				jq.closest(".ui-jqgrid-bdiv").css({ 'overflow-x' : 'scroll' });
			}
						
			return jq;
		},
		treeGrid: function(options, params) {
			var settings = $.extend({}, this.defaults, this.treeOptions, options);
			
			var jq = this.$.jqGrid(settings);
			if(!$.support.leadingWhitespace) {
				jq.closest(".ui-jqgrid-bdiv").css({ 'overflow-x' : 'scroll' });
			}
			return jq;
		},
		searchGrid: function(form) {
			var objs = form.serializeObject();
			this.$.jqGrid('setGridParam', {postData: objs});
			this.$.jqGrid("setGridParam",{page:1}).trigger("reloadGrid");
			
			return false;
		},
		customizeColumn: function(options) {
			var util = this;
			var jq = this.$;
			// clone array
			var defaultColModel = $.parseJSON(JSON.stringify(jq.getGridParam("colModel")));
			// get name index
			var defaultColIndex = {};
			$.each(defaultColModel, function(i, n) {
				defaultColIndex[n.name] = i;
			});
			
			jq.jqGrid('navGrid',jq.getGridParam("pager"),{edit:false,add:false,del:false,search:false,refresh:true},{},{},{top:1000,left:1000});
			
			var settings = $.extend({
				caption: '自定义显示列',
				title: '自定义显示列',
				onClickButton :	function (){
					jq.jqGrid('columnChooser', util.columnOpts);
				}
			}, options || {});

			jq.jqGrid('navButtonAdd',jq.getGridParam("pager"),settings);
			
			var settings2 = $.extend({
				caption: '还原为默认列',
				title: '还原为默认列',
				onClickButton :	function (){
					// remote
					$.ajax({
						type: "POST",
						dataType: "json",
						url: $.commonUtil.contextPath() + "framework/gridColumn-delete.action",
						data: { 
							gridId: util.getGridId()
						}
					});
					
					// local
					var perm = new Array();
					var colIndex = {};
					var cols = jq.getGridParam("colModel");
					// sort index
					$.each(cols, function(i, n) {
						colIndex[n.name] = i;
					});
					// show and hide
					$.each(defaultColModel, function(i, n){
						if (n.hidden)
							jq.jqGrid("hideCol", n.name);
						else
							jq.jqGrid("showCol", n.name);
						perm.push(colIndex[n.name]);
					});
					// sort
					jq.jqGrid("remapColumns", perm, true);
				}
			}, options || {});
			jq.jqGrid('navButtonAdd',jq.getGridParam("pager"),settings2);
			
			// load customized columns
			$.ajax({
				type:"POST",
				dataType: "json",
				url: $.commonUtil.contextPath() + "framework/gridColumn-query.action",
				data: {
					gridId: util.getGridId()
				},
				success:function(result){
					try {
						if (result != null && result.content != null) {
							
							// parse customize data
							var cols = JSON.parse(result.content);
							var perm = new Array();
							
							// if new version modify colModel, will not use customize column
							if (cols.length != defaultColModel.length)
								return;
							
							for(var i = 0; i < cols.length; ++i) {
								if (cols[i].hidden)
									jq.jqGrid("hideCol", cols[i].name);
								else
									jq.jqGrid("showCol", cols[i].name);
								
								var idx = defaultColIndex[cols[i].name];
								if (typeof(idx) == 'undefined' || idx == null)
									return;	// colModel modified, return
								
								perm.push(idx);
							}
							
							jq.jqGrid("remapColumns", perm, true);
						}
					} catch(e) {}
					
				}
			});
			
			return jq;
		},
		exportGridExcel: function(form, options) {
			var objs = form.serializeObject();
			this.$.jqGrid('setGridParam', {postData: objs});
			this.exportExcel(options);
			
			return false;
		},
		// 根据上一次查询结果进行导出
		exportExcel: function(options, callback) {
			if (options.url.indexOf("loadPageCache=true") >= 0) {
				var rowData = this.$.jqGrid('getRowData');
				if (typeof(rowData) == 'undefined' || rowData == null  || rowData.length < 1) {
					$.boxUtil.alert('下载数据为空，请重新过滤查询条件！');
					return false;
				}
			}
			
			var postColModel = new Array();
			
			var colNames = this.$.getGridParam("colNames");
			var colModel = this.$.getGridParam("colModel");
			
			$.each(colModel, function(i, n){
				if(typeof(n['index']) != 'undefined' && n['index'] != null) {
					// && !n['hidden']
					postColModel.push(n);
				}
			});
			
			var params = $.extend({}, {"exportParams.colModel": JSON.stringify(postColModel)}, this.$.getGridParam("postData"));
			//console.log(params);
			
			if (false != options.openWindow)
				$.commonUtil.downloadWindow(options.url, params, "post", callback);
			else
				$.commonUtil.download(options.url, params, "post", callback);
			
			//if(options.url.indexOf("?") != -1) { url = options.url+"&"+params; }
			//else { url = options.url+"?"+params; }
			//window.location = encodeURI(url);
		},
		getGridId : function(self){
			if (typeof(self) == 'undefined' || self == null)
				self = this.$;
			
			return this.columnOpts.grid_id(self);
		},
		columnOpts : {
			"grid_id" : function(self) {
				var gridId = window.location.pathname + "/#" + self[0].id;
				
				return gridId;
			},
			"customize" : function(self) {
				var colModel = self.jqGrid("getGridParam", "colModel");
				var gridId = this.grid_id(self);
				
				// save customized columns
				$.ajax({
					type:"POST",
					dataType: "json",
					url: $.commonUtil.contextPath() + "framework/gridColumn-save.action",
					data: {
						gridId: gridId,
						content: JSON.stringify(colModel)
					}
				});

			},
			"dlog_opts" : function(opts) {
				var self = this;

                var buttons = {};
				buttons['保存自定义列'] = function() {
                    opts.apply_perm();
                    opts.cleanup(false);
					opts.customize(self);
                };
                buttons[opts.bSubmit] = function() {
                    opts.apply_perm();
                    opts.cleanup(false);
                };
                buttons[opts.bCancel] = function() {
                    opts.cleanup(true);
                };
                return {
                    "buttons": buttons,
                    "close": function() {
                        opts.cleanup(true);
                    },
					"modal" : false,
                    "resizable": false,
                    "width": opts.width+20
                };
            }
		}
		
	};
	
	var _loadComplete = function(result) {
		if(result.success == false) {
			if (typeof result.message != 'undefined' && result.message != null && result.message.indexOf("未登录系统") > -1) {
				window.location.href = $.commonUtil.contextPath();
			} else {
				$.boxUtil.error(result.message);
			}
		}
	};
	
	// .commonUtil interface
	$.commonUtil = { };
	
	$.commonUtil.contextPath = function() {
		var pathname = window.location.pathname;
		var contextPath = pathname.substr(0, pathname.indexOf("/", 1) + 1);
		return contextPath;
	};
	
	$.commonUtil.unparam = function(p) {
		var ret = {},
			seg = p.replace(/^.*\?/,'').split('&'),
			len = seg.length, i = 0, s;
		for (;i<len;i++) {
			if (!seg[i]) { continue; }
			s = seg[i].split('=');
			ret[s[0]] = s[1];
		}
		return ret;
	};
	
	$.commonUtil.download = function(url, data, method, callback) {
		var inputs = "";
		var iframeX;

		if(url && data){
			$.blockUI({
				showOverlay: false,
				message: '<div class="loading_img"><span class="loading_span">正在下载，请稍后...</span></div>',
				css: {padding: '20px'}
			});

			// remove old iframe if has
			if($("#iframeX")) $("#iframeX").remove();
			// creater new iframe
			iframeX = $("<iframe style='display:none;' src='javascript:false;' name='iframeX' id='iframeX'></iframe>").appendTo("body").hide();
			
			//split params into form inputs
			$.each(data, function(p, val){
				var value = val;
				if (val != null && typeof(val) == 'string') {
					value = val.replace(/"/g, "&quot;");
				}
				inputs += "<input type='hidden' name='"+ p +"' value=\""+ value +"\" />";
			});
			
			if(!$.support.leadingWhitespace){
				iframeX.get(0).onreadystatechange = function() {
					if ( iframeX.get(0).readyState == "loading" ) {	// loading state
						$.unblockUI();
					} else if ( iframeX.get(0).readyState == "complete" ) {
						_finish(iframeX, form, callback);
					}
				};
			} else if ($.browser.mozilla || $.browser.webkit){
				iframeX.get(0).onload = function() {
					$.unblockUI();
					_finish(iframeX, form, callback);
				};
			} else {	// do not support chrome yet
				setTimeout($.unblockUI,	2000);
			}

			if (typeof method == 'undefined' || method == null)
				method = "POST";
			//create form to send request
			var form = $("<form action='" + url + "' method='" + method + "' target='iframeX'>" + inputs + "</form>").appendTo("body");
			
			form.submit();
			form.remove();
		};
	};

	$.commonUtil.downloadWindow = function(url, data, method, callback) {
		var inputs = "";

		if(url && data){
			
			//split params into form inputs
			$.each(data, function(p, val){
				var value = val;
				if (val != null && typeof(val) == 'string') {
					value = val.replace(/"/g, "&quot;");
				}
				inputs += "<input type='hidden' name='"+ p +"' value=\""+ value +"\" />";
			});
			
			if (typeof method == 'undefined' || method == null)
				method = "POST";
			//create form to send request
			var form = $("<form action='" + url + "' method='" + method + "' target='_blank'>" + inputs + "</form>").appendTo("body");
			
			form.submit();
			form.remove();
		};
	};
	
	var _finish = function(iframeX, form, callback) {
		$.unblockUI();
		
		/*
		setTimeout(function(){
			//form.remove();
			iframeX.remove();
		}, 100);
		*/
		
		if (typeof(callback) == 'function')
			callback();
	};

})(jQuery);
